﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;

namespace WorldOfMUDra
{
    public static class Globals
    {
        public static PasswordStorageFile PassFile = new PasswordStorageFile("userpasses.txt");

        public static World Mudra;

        public static string RemoveExtraSpaces(string input)
        {
            return Regex.Replace(input, @"\s+", " ");
        }

        public static void CreateConfigFor(Player noob)
        {
            List<string> playerinfo = new List<string> {"<Player>", "<Name>", noob.Name, "</Name>"};

            if (noob.LoggedIn)
            {
                playerinfo.Add("<Location>");
                playerinfo.Add(noob.LocationId);
                playerinfo.Add("</Location>");

                playerinfo.Add("<VisitedAreas>");
                playerinfo.Add(noob.VisitedAreas);
                playerinfo.Add("</VisitedAreas>");

                playerinfo.Add("<ItemIDs>");
                playerinfo.Add(noob.ItemIDs);
                playerinfo.Add("</ItemIDs>");

                playerinfo.Add("<Abilities>");
                playerinfo.Add(noob.GainedAbilities);
                playerinfo.Add("</Abilities>");
            }

            playerinfo.Add("</Player>");

            string fname = noob.Name + ".xml";
            try
            {
                StreamWriter sw = File.CreateText(fname);
                sw.Close();
                File.WriteAllLines(fname, playerinfo);
            }
            catch
            {
                Console.WriteLine("Error creating user config. Make sure the file is not opened in another program and that you have permission to access it.");
            }
            
        }

    }
}
